package com.pingan.stat.reposys.money24.service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import com.pingan.common.core.property.Properties;
import com.pingan.common.util.ShellExecutor;
import com.pingan.setting.money24url.entity.StatProjChannelBean;
import com.pingan.setting.money24url.entity.StatProjDefineBean;
import com.pingan.setting.money24url.entity.StatProjPageBean;
import com.pingan.setting.money24url.service.StatProjDefineSettingService;
import com.pingan.stat.service.StatService;

@Component
public class LoanPagesStatService implements StatService {

	@Properties(name = "reposys.loan.pages.output.path")
	private String outputPath;

	@Properties(name = "reposys.loan.pages.stat.hql.file")
	private String loanPagesStatCreateHQLShell;

	@Properties(name = "reposys.loan.pages")
	private String loanPagesStatShell;

	@Resource
	private StatProjDefineSettingService statProjDefineSettingService;

	@Resource
	private ShellExecutor shellExecutor;

	public void dailyStat(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String yyyyMMdd = sdf.format(date);
		String output = outputPath + "/" + yyyyMMdd;
		List<StatProjDefineBean> projects = statProjDefineSettingService
				.getStatableProjects();

		for (StatProjDefineBean project : projects) {
			List<StatProjChannelBean> channels = statProjDefineSettingService
					.getStatChanels(project.getId());

			for (StatProjChannelBean channel : channels) {
				StringBuilder sql = new StringBuilder("select ");
				List<StatProjPageBean> pages = statProjDefineSettingService
						.getPageBeans(project.getId());

				sql.append(getSelectPart(channel, pages))
						.append(",")
						.append("count(*),count(distinct(juid)) from "
								+ project.getSiteId() + ".pv ")
						.append("where log_date='").append(yyyyMMdd)
						.append("' and ").append(getWherePart(channel, pages))
						.append(" group by ")
						.append(getSelectPart(channel, pages));

				String[] hqlParams = new String[] { sql.toString() };
				shellExecutor.excecute(loanPagesStatCreateHQLShell, hqlParams);
				System.out.println(sql.toString());
				String[] params = new String[] { yyyyMMdd, output,
						Integer.valueOf(project.getId()).toString(),
						Integer.valueOf(channel.getId()).toString() };

				shellExecutor.excecute(loanPagesStatShell, params);
			}
		}
	}

	public String getSelectPart(StatProjChannelBean channel,
			List<StatProjPageBean> pages) {
		StringBuilder sb = new StringBuilder("(case ");
		String code = channel.getChannelCode();
		int index = 1;
		for (StatProjPageBean page : pages) {
			StringBuilder urlClause = new StringBuilder();

			if (index <= pages.size()) {
				String url = page.getUrl();

				if (!StringUtils.isBlank(code) && !StringUtils.isEmpty(code)) {
					urlClause.append(" when url like '").append(url)
							.append(getJoint(channel.getProjId()))
							.append(parseCode(code)).append("%' then '")
							.append(Integer.valueOf(page.getId()).toString())
							.append("' ");
				} else {
					urlClause.append(" when url like '").append(url)
							.append("%' then '")
							.append(Integer.valueOf(page.getId()).toString())
							.append("' ");
				}

			}
			sb.append(urlClause.toString());
			index++;
		}

		sb.append("else 'else' END)");
		return sb.toString();
	}

	public String getWherePart(StatProjChannelBean channel,
			List<StatProjPageBean> pages) {
		StringBuilder sb = new StringBuilder("( ");
		String code = channel.getChannelCode();
		int index = 1;
		for (StatProjPageBean page : pages) {
			StringBuilder urlClause = new StringBuilder();

			if (index == 1) {
				String url = page.getUrl();

				if (!StringUtils.isBlank(code) && !StringUtils.isEmpty(code)) {
					urlClause.append(" url like '").append(url)
							.append(getJoint(channel.getProjId()))
							.append(parseCode(code)).append("%' ");
				} else {
					urlClause.append(" url like '").append(url).append("%' ");
				}
			} else if (index > 1 && index <= pages.size()) {
				String url = page.getUrl();

				if (!StringUtils.isBlank(code) || !StringUtils.isEmpty(code)) {
					urlClause.append(" or url like '").append(url)
							.append(getJoint(channel.getProjId()))
							.append(parseCode(code)).append("%' ");
				} else {
					urlClause.append(" or url like '").append(url)
							.append("%' ");
				}
			}

			sb.append(urlClause.toString());
			index++;
		}

		sb.append(")");

		return sb.toString();
	}

	public String parseCode(String code) {
		// String[] array = StringUtils.splitByWholeSeparatorPreserveAllTokens(
		// code, "&");
		// StringBuilder sb = new StringBuilder();
		// for (String s : array) {
		// if (!StringUtils.isBlank(s) && !StringUtils.isEmpty(s)) {
		// sb.append(s).append("%");
		// }
		// }
		//
		// return sb.toString();

		return code;

	}

	public String getJoint(int projId) {
		if (projId == 3 || projId == 4) {
			return "&";
		} else {
			return "%";
		}
	}
}
